package com.jp.knowledge.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.View;

import com.jp.knowledge.R;
import com.jp.knowledge.model.HeadLineData;
import com.jp.knowledge.my.base.RecycleVAdapter;
import com.jp.knowledge.my.base.RecycleVHolder;
import com.jp.knowledge.util.ImageLoadUtil;
import com.jp.knowledge.util.TimeUtil;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;

/**
 * Created by 528787067@qq.com on 2017/7/17.
 */

public class PositionToolsAdapter extends RecycleVAdapter<HeadLineData> implements View.OnClickListener {

//	private DateFormat dateFormat;

	public PositionToolsAdapter(Context context, @NonNull List<HeadLineData> list) {
		super(context, list);
//		dateFormat = new SimpleDateFormat("yyyy年MM月dd日", Locale.getDefault());
	}

	@Override
	public int getLayoutId(int viewType) {
		return R.layout.item_position_tools_main;
	}

	@Override
	public void onBind(RecycleVHolder holder, int position) {
		HeadLineData data = getData(position);
		holder.setTextView(R.id.position_tools_title, data.getTitle());
		holder.setTextView(R.id.position_tools_desc, data.getDesc());
		holder.setImageUrl(R.id.position_tools_icon, data.getIcon());
//		holder.setTextView(R.id.position_tools_time, dateFormat.format(data.getCreatetime()));
		holder.setTextView(R.id.position_tools_time, TimeUtil.friendly_time(data.getCreatetime()));

		View itemMainView = holder.get(R.id.main_view);
		itemMainView.setTag(position);
		itemMainView.setOnClickListener(this);
		View cancelBtn = holder.get(R.id.cancel_btn);
		cancelBtn.setTag(position);
		cancelBtn.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if(v.getId() == R.id.main_view && itemClickEventListener != null){
			itemClickEventListener.onMainItemClick((int)v.getTag());
		} else if(v.getId() == R.id.cancel_btn && itemClickEventListener != null){
			itemClickEventListener.onCancelBtnClick((int)v.getTag());
		}
	}

	private ItemClickEventListener itemClickEventListener;

	public void setItemClickEventListener(ItemClickEventListener itemClickEventListener){
		this.itemClickEventListener = itemClickEventListener;
	}

	public interface ItemClickEventListener{
		void onMainItemClick(int position);
		void onCancelBtnClick(int position);
	}
}
